package nuc.soft.xsf.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import nuc.soft.xsf.model.Role;


import nuc.soft.xsf.model.User;
import nuc.soft.xsf.service.IRoleService;
import nuc.soft.xsf.tools.Tools;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/RoleController")
public class RoleController {
   @Resource
   private IRoleService roleService;
   
   @RequestMapping(value="/add.action" ,produces="text/html;charset=UTF-8")
   @ResponseBody
   public String addRole(Role role){
	   role.setRoleState(0);
	   roleService.insert(role);
	   JSONObject json=new JSONObject();
	   json.put("data", role);
	   return json.toString();
   }
   
   @RequestMapping(value="/findByCondition.action",produces="text/html;charset=utf-8")
   @ResponseBody
   public String finByCondition(Role role){
	   Map map=Tools.transBean2Map(role);
	   List<Role> list=roleService.findByCondition(map);
	   JSONObject json=new JSONObject();
	   json.put("list", list);
	   return json.getString("list");
   }
   
   
   @RequestMapping(value="/update.action",produces="text/html;charset=utf-8")
   @ResponseBody
   public String update(Role role){
	   roleService.update(role);
	   Role role1=roleService.findById(role.getRoleId());
	   JSONObject json=new JSONObject();
	   json.put("data", role1);
	   json.put("redata", role1.getRoleId());
	   return json.toString();
   }
   
   
   
   @RequestMapping(value="/delete.action",produces="text/html;charset=utf-8")
   @ResponseBody
   public String delete(String ids){
   	    String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			Role role = roleService.findById(Integer.parseInt(strTmp));
			if (role != null) {
			    role.setRoleState(1);
				roleService.update(role);
				success++;
			}
		}
		json.put("success", success);
		return json.toString();
	}
   	
}
